package com.engine.salary.wrapper;

import cn.hutool.core.util.StrUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.entity.SalarySobExtRangePO;
import com.engine.salary.entity.salarysob.dto.SalarySobRangeListDTO;
import com.engine.salary.entity.salarysob.param.SalarySobRangeExtSaveParam;
import com.engine.salary.entity.salarysob.param.SalarySobRangeImportParam;
import com.engine.salary.entity.salarysob.param.SalarySobRangeQueryParam;
import com.engine.salary.entity.salarysob.param.SalarySobRangeSaveParam;
import com.engine.salary.service.SalarySobExtRangeService;
import com.engine.salary.service.SalarySobRangeService;
import com.engine.salary.service.impl.SalarySobExtRangeServiceImpl;
import com.engine.salary.service.impl.SalarySobRangeServiceImpl;
import com.engine.salary.util.page.PageInfo;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

/**
 * 薪资账套的人员范围
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalarySobRangeWrapper extends Service {

    private SalarySobRangeService getSalarySobRangeService(User user) {
        return ServiceUtil.getService(SalarySobRangeServiceImpl.class, user);
    }

    private SalarySobExtRangeService getSalarySobExtRangeService(User user) {
        return ServiceUtil.getService(SalarySobExtRangeServiceImpl.class, user);
    }

    /**
     * 薪资账套的人员范围列表（关联人员范围）
     *
     * @param queryParam 列表查询条件
     * @return
     */
    public PageInfo<SalarySobRangeListDTO> listPage4Include(SalarySobRangeQueryParam queryParam) {
        return getSalarySobRangeService(user).listPageByParamAndIncludeType(queryParam, NumberUtils.INTEGER_ONE);
    }

    /**
     * 薪资账套的人员范围列表（从范围中排除）
     *
     * @param queryParam 列表查询条件
     * @return
     */
    public PageInfo<SalarySobRangeListDTO> listPage4Exclude(SalarySobRangeQueryParam queryParam) {
        return getSalarySobRangeService(user).listPageByParamAndIncludeType(queryParam, NumberUtils.INTEGER_ZERO);
    }

    /**
     * 获取薪资账套的人员范围列表添加人员的表单
     *
     * @return
     */
//    public WeaForm getForm() {
//        return SalaryFormatUtil.<SalarySobRangeFormDTO>getInstance().buildForm(SalarySobRangeFormDTO.class, new SalarySobRangeFormDTO());
//    }

    /**
     * 保存
     *
     * @param saveParam 保存参数
     */
    public void save(SalarySobRangeSaveParam saveParam) {
        Optional.ofNullable(saveParam.getTargetParams()).orElse(new ArrayList<>())
                .forEach(targetParam -> {
                    if (StrUtil.isNotBlank(targetParam.getTarget())) {
                        targetParam.setTarget(targetParam.getTarget()
                                .replace("ｓｅｌｅｃｔ", "select")
                                .replace("ＳＥＬＥＣＴ", "SELECT")
                                .replace("ｊｏｉｎ", "join")
                                .replace("ＪＯＩＮ", "JOIN")
                                .replace("ａｎｄ", "and")
                                .replace("ＡＮＤ", "AND")
                                .replace("ｏｒ", "or")
                                .replace("ＯＲ", "OR")
                                .replace("ｉｎ", "in")
                                .replace("ＩＮ", "IN")
                                .replace("ｌｉｋｅ", "like")
                                .replace("ＬＩＫＥ", "LIKE")
                                .replace("ｅｘｉｓｔｓ", "exists")
                                .replace("ＥＸＩＳＴＳ", "EXISTS")
                                .replace("ｂｅｔｗｅｅｎ", "between")
                                .replace("ＢＥＴＷＥＥＮ", "BETWEEN")
                                .replace("ｕｎｉｏｎ", "union")
                                .replace("ＵＮＩＯＮ", "UNION")
                                .replace("ｓｕｂｓｔｒ", "substr")
                                .replace("ＳＵＢＳＴＲ", "SUBSTR")
                        );
                    }
                });

        getSalarySobRangeService(user).save(saveParam);
    }


    public void edit(SalarySobRangeSaveParam param) {
        Optional.ofNullable(param.getTargetParams()).orElse(new ArrayList<>())
                .forEach(targetParam -> {
                    if (StrUtil.isNotBlank(targetParam.getTarget())) {
                        targetParam.setTarget(targetParam.getTarget()
                                .replace("ｓｅｌｅｃｔ", "select")
                                .replace("ＳＥＬＥＣＴ", "SELECT")
                                .replace("ｊｏｉｎ", "join")
                                .replace("ＪＯＩＮ", "JOIN")
                                .replace("ａｎｄ", "and")
                                .replace("ＡＮＤ", "AND")
                                .replace("ｏｒ", "or")
                                .replace("ＯＲ", "OR")
                                .replace("ｉｎ", "in")
                                .replace("ＩＮ", "IN")
                                .replace("ｌｉｋｅ", "like")
                                .replace("ＬＩＫＥ", "LIKE")
                                .replace("ｅｘｉｓｔｓ", "exists")
                                .replace("ＥＸＩＳＴＳ", "EXISTS")
                                .replace("ｂｅｔｗｅｅｎ", "between")
                                .replace("ＢＥＴＷＥＥＮ", "BETWEEN")
                                .replace("ｕｎｉｏｎ", "union")
                                .replace("ＵＮＩＯＮ", "UNION")
                                .replace("ｓｕｂｓｔｒ", "substr")
                                .replace("ＳＵＢＳＴＲ", "SUBSTR")
                        );
                    }
                });

        getSalarySobRangeService(user).edit(param);
    }

    /**
     * 保存
     *
     * @param saveParam 保存参数
     */
    public void saveExtRange(SalarySobRangeExtSaveParam saveParam) {
        getSalarySobExtRangeService(user).saveExtRange(saveParam);
    }

    public PageInfo<SalarySobExtRangePO> listPage4Ext(SalarySobRangeQueryParam param) {
        return getSalarySobExtRangeService(user).listPage4Ext(param);
    }

    /**
     * 删除
     *
     * @param ids 薪资账套的人员范围的主键id
     */
    public void delete(Collection<Long> ids) {
        getSalarySobRangeService(user).deleteByIds(ids);
    }

    public void deleteSalarySobExtRange(Collection<Long> ids) {
        getSalarySobExtRangeService(user).deleteSalarySobExtRange(ids);
    }

    /***
     * @description 下载薪资账套人员范围导入模板
     * @return XSSFWorkbook
     * @author Harryxzy
     * @date 2023/1/9 11:32
     */
    public XSSFWorkbook exportImportTemplate() {
        return getSalarySobRangeService(user).downloadTemplateRange();
    }

    /**
     * @return void
     * @description 薪资账套人员范围导入预览
     * @author Harryxzy
     * @date 2023/1/9 13:37
     */
    public Map<String, Object> preview(SalarySobRangeImportParam importParam) {
        return getSalarySobRangeService(user).preview(importParam);
    }

    /**
     * @return Map<String, Object>
     * @description 导入薪资账套人员范围
     * @author Harryxzy
     * @date 2023/1/9 14:10
     */
    public Map<String, Object> importData(SalarySobRangeImportParam importParam) {
        return getSalarySobRangeService(user).importData(importParam);
    }
}
